﻿using Andon.Utils.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace HttpDemo.Utils
{
    public class GetDataHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext aHttpContext)
        {
            string data = string.Empty;
            string receive = string.Empty;
            try
            {
                data = aHttpContext.Request.Form.Get("input");
                if (string.IsNullOrEmpty(data))
                {
                    data = "NoReceiveData";
                }
                else
                {
                    if (data.Contains('@'))
                    {

                        FileUtil.UpdateFile(data.TrimStart('@'), ConfigurationUtil.GetAppSettingValue("saveport"), Encoding.UTF8);
                    }
                    else
                    {
                        receive = FileUtil.ReadFile(ConfigurationUtil.GetAppSettingValue("saveport"), Encoding.UTF8);
                    }
                }
                
            }
            catch (Exception ex)
            {
                data = "error";
            }

            string message = string.Empty;
            message += ConfigurationUtil.GetAppSettingValue("port");
            message += "&";
            message += data;
            message += "&";
            message += DateTime.Now;
            message += "\r\n";

            FileUtil.AppendFile(message, ConfigurationUtil.GetAppSettingValue("path"), Encoding.UTF8);

            aHttpContext.Response.Write(receive);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
